package com.miniapp.service.impl;

import com.miniapp.entity.MiniUserProductEntity;
import com.miniapp.service.MiniUserProductService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author linyuchi
 */
@Service
public class WxOpenService extends WxOpenServiceImpl {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private MiniUserProductService miniUserProductService;
    private WxOpenMessageRouter wxOpenMessageRouter;

    @Value("${wechat.component.app_id}")
    private String componentAppId ;
    @Value("${wechat.component.token}")
    private String componentToken ;
    @Value("${wechat.component.aes_key}")
    private String componentAesKey ;
    @Value("${wechat.component.secret}")
    private String componentSecret ;

    @PostConstruct
    public void init() {
        WxOpenInRedisConfigStorage inRedisConfigStorage = new WxOpenInRedisConfigStorage(jedisPool);
        inRedisConfigStorage.setComponentAppId(componentAppId);
        inRedisConfigStorage.setComponentAppSecret(componentSecret);
        inRedisConfigStorage.setComponentToken(componentToken);
        inRedisConfigStorage.setComponentAesKey(componentAesKey);
        setWxOpenConfigStorage(inRedisConfigStorage);
        wxOpenMessageRouter = new WxOpenMessageRouter(this);
        wxOpenMessageRouter.rule().handler(new WxMpMessageHandler() {
            @Override
            public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
                logger.info("\n接收到 {} 公众号请求消息，内容：{}", wxMpService.getWxMpConfigStorage().getAppId(), wxMpXmlMessage);
                return null;
            }
        }).next();
    }
    public WxOpenMessageRouter getWxOpenMessageRouter(){
        return wxOpenMessageRouter;
    }

    public String getAccessTokenByAppid(String appid) {
        MiniUserProductEntity miniUserProduct = miniUserProductService.findByAppid(appid);
        String accessToken ="";
        try {
            accessToken = this.getWxOpenComponentService().getAuthorizerAccessToken(miniUserProduct.getWxAppid(),true);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return accessToken;
    }
}